//    Copyright (C) Kherty.  All rights reserved.
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet
{
    public class SortCondition: IOfficeXmlSerializable
    {
        public SortCondition(SortState parent)
        {
            this.parent = parent;
        }

        readonly SortState parent;
        public SortState Parent { get { return parent; } }
        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read("sortCondition"))
            {
                descending = c.GetOptionalBoolean("descending", false);
                sortBy = c.GetOptionalEnum("sortBy", SortBy.Value);
                Reference = c.GetString("ref");
                customList = c.GetOptionalString("customList");
                dxfId = c.GetOptionalInteger("dxfId");
                iconSet = c.GetOptionalEnum("iconSet", IconSetType.ThreeArrows);
                iconId = c.GetOptionalInteger("iconId");
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("sortCondition"))
            {
                c.SetOptionalBoolean("descending", descending, false);
                c.SetOptionalEnum("sortBy", sortBy, SortBy.Value);
                c.SetString("ref", Reference);
                c.SetOptionalString("customList", customList);
                c.SetOptionalInteger("dxfId", dxfId);
                c.SetOptionalEnum("iconSet", iconSet, IconSetType.ThreeArrows);
                c.SetOptionalInteger("iconId", iconId);
            }
        }

        #endregion

        private int _index;

        public int Index
        {
            get { return _index; }
            set { _index = value; }
        }
	
        private bool descending;

        public bool Descending
        {
            get { return descending; }
            set { descending = value; }
        }
        private SortBy sortBy;

        public SortBy SortBy
        {
            get { return sortBy; }
            set { sortBy = value; }
        }
        private string _ref;

        public string Reference
        {
            get { return _ref; }
            set { _ref = value; }
        }
        private string customList;

        public string CustomList
        {
            get { return customList; }
            set { customList = value; }
        }
        private int? dxfId;

        public int? DxfId
        {
            get { return dxfId; }
            set { dxfId = value; }
        }
        private IconSetType iconSet;

        public IconSetType IconSet
        {
            get { return iconSet; }
            set { iconSet = value; }
        }
        private int? iconId;

        public int? IconId
        {
            get { return iconId; }
            set { iconId = value; }
        }




        internal int Compare(PartialRowOrColumnRange r1, PartialRowOrColumnRange r2)
        {
            if (Descending)
                return CellComparator.CompareCellsDown(r1.Cells[Index], r2.Cells[Index]);
            return CellComparator.CompareCellsUp(r1.Cells[Index], r2.Cells[Index]);
        }
        public string Label
        {
            get
            {
                Cell topLeft = parent.Range.TopLeftCell;
                if (parent.SortByColumn)
                {
                    return topLeft.Worksheet.Rows[topLeft.Row.Index + Index].Label;
                }
                return topLeft.Worksheet.Columns[topLeft.Column.Index + Index].Label;
            }
        }

        internal SortCondition Clone()
        {
            SortCondition result = (SortCondition)MemberwiseClone();
            return result;
        }
    }
}
